var express = require('express');
var router = express.Router();
const userApi = require('../api/users')
const { md5 } = require("../util/salt");

/* GET home page. */
//获取用户列表
router.get('/get', async function (req, res, next) {
    const { page, limit } = req.query;
    let data = await userApi.get({ page, limit });
    res.send(data)
});
//登陆
router.get('/login', async function (req, res, next) {
    const { code, nickName, img, appid, secret } = req.query;
    let data = await userApi.login({ code, nickName, img, appid, secret });
    res.send(data)
});
//注册
router.post('/reg', async function (req, res, next) {
    const { nickName, userName, userPassword, img = [] } = req.query;
    let data = await userApi.reg({ nickName, userName, userPassword, img });
    res.send(data)
});
//更新
router.put('/update/:_id', async function (req, res, next) {
    const { _id } = req.params;
    const { nickName, userName, userPassword, status } = req.query;
    let data = await userApi.update({ _id }, { nickName, userName, userPassword: md5(userPassword), status });
    res.send(data)
});
//更新
router.patch('/update/:_id', async function (req, res, next) {
    const { _id } = req.params;
    const { nickName, userName, userPassword, status } = req.query;
    let data = await userApi.update({ _id }, { nickName, userName, userPassword: md5(userPassword), status });
    res.send(data)
});
//删除
router.delete('/del/:_id', async function (req, res, next) {
    const { _id } = req.params;
    let data = await userApi.del({ _id });
    res.send(data)
});
//搜索
router.get('/search', async function (req, res, next) {
    let data = await userApi.search(req.query);
    res.send(data)
});

//给用户添加img
router.patch('/:_id/img/:url', async function (req, res, next) {
    let { _id, url } = req.params;
    let data = await userApi.addImg({ _id, url });
    res.send(data)
});
//删除用户img
router.delete('/:_id/img/:url', async function (req, res, next) {
    let { _id, url } = req.params;
    let data = await userApi.removeImg({ _id, url });
    res.send(data)
});
//添加收货地址
router.put('/:_id/address', async function (req, res, next) {
    const { _id } = req.params;
    const { name, phone, address } = req.body;
    console.log( _id,req.body)
    const data = await userApi.addAddress({_id, name, phone, address });
    res.send(data)
})
module.exports = router;
